


function getQueryString(name) {
    let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    let r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return unescape(r[2]);
    };
    return 0;
 }


var data = [];

$("body").find("div").each(function(e){
    var indexid=$(this).attr("id")+"";
    if (indexid.indexOf("searchResultsV2-") != -1 ){
        console.log($(this).attr("id"));
        body = $(this).attr("data-state");
        console.log(body);
        var type="ozonfirstpage"; //抓分页
        //var type="ozonspuimg";//抓图片匹配用的库
        var path =  window.location.pathname;
        var segments = path.split('/');
        var category = segments[segments.length - 2];
        var page = parseInt(getQueryString("page"));
        if(page<=0){
            page = 1;
        }
         console.log(category);
         console.log(page);
         if(body){
            chrome.runtime.sendMessage({ data: {type,body,category}, close: false });

            if(page < 200){
                setTimeout(() => {

                    // 获取当前URL
                    var url = window.location.href;
                    // 判断 URL 是否包含 page=xx
                    if (url.includes('page=')) {
                      // 使用正则表达式替换字符串
                      url = url.replace(/(page=)(\d+)/g, function(match, p1, p2) {
                        return p1 + (parseInt(p2) + 1); // 将页码加 1
                      });
                    } else {
                      // 拼接 page=1
                      url += (url.includes('?') ? '&' : '?') + 'page=2';
                    }

                    location.href=url;
                }, 20000);
            }

         }else{
            alert("no data")
         }

    };
})


